package com.xx.landing;

import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import java.util.Objects;

import static com.xx.util.Util.getResourceUrl;


public class Input {
    public HBox hb;
    private boolean right;
    private ImageView icon;
    private ImageView close;
    private ImageView eye;
    public TextField textField;
    public PasswordField passwordField;
    private ColorAdjust iconAdjust = new ColorAdjust();
    private ColorAdjust closeAdjust = new ColorAdjust();
    private ColorAdjust eyeAdjust = new ColorAdjust();
    private Label label;

    //      普通输入框.
    public Input(ImageView icon, ImageView close) {
        this.hb = new HBox();
        this.hb.getStyleClass().add("hb-input");
        this.textField = new TextField();
        textField.setFocusTraversable(false);
        this.icon = icon;
        icon.setPreserveRatio(true);
        icon.setFitWidth(50);
        close.setPreserveRatio(true);
        close.setFitWidth(30);
        this.close = close;
        Rectangle rc = new Rectangle(30, 30);
        rc.setFill(Color.TRANSPARENT);
//        rc可以写个下拉列表之类的代替,可以自己拓展.现在的rc是填充作用,与eye大小一定要相等.
        this.hb.getChildren().addAll(icon, textField, close, rc);
        icon.setEffect(iconAdjust);
        close.setEffect(closeAdjust);
        close.setVisible(false);
        adjustClose(false);
        adjustIcon(false);
        initEvent();
    }

    //       密码框实现.
    public Input(ImageView icon, ImageView close, ImageView eye) {
        this.hb = new HBox();
        this.hb.getStyleClass().add("hb-input");
        this.textField = new TextField();
        textField.setFocusTraversable(false);
        this.passwordField = new PasswordField();
        passwordField.setFocusTraversable(false);
        this.icon = icon;
        icon.setPreserveRatio(true);
        icon.setFitWidth(50);
        this.close = close;
        close.setPreserveRatio(true);
        close.setFitWidth(30);
        this.eye = eye;
        eye.setPreserveRatio(true);
        eye.setFitWidth(30);
        label = new Label();
        label.setPrefSize(eye.getFitWidth(), eye.getFitHeight());
        label.setGraphic(eye);
        StackPane sp = new StackPane();
        sp.getChildren().addAll(passwordField, textField);
        textField.setVisible(false);
        this.hb.getChildren().addAll(icon, sp, close, label);
        icon.setEffect(iconAdjust);
        close.setEffect(closeAdjust);
        eye.setEffect(eyeAdjust);
        close.setVisible(false);
        adjustClose(false);
        adjustIcon(false);
        adjustEye(false);
        initPassword();
    }

    public void setPromptText(String s) {
        textField.setPromptText(s);
        if (passwordField != null) passwordField.setPromptText(s);
    }

    private void initPassword() {
        passwordField.textProperty().addListener((ob, ov, nv) -> {
            close.setVisible(!Objects.equals(nv, ""));
        });
        textField.textProperty().addListener((ob, ov, nv) -> {
            close.setVisible(!Objects.equals(nv, ""));
        });
        close.setOnMouseEntered(event -> adjustClose(true));
        close.setOnMouseExited(event -> adjustClose(false));
        close.setOnMouseClicked(event -> {
            passwordField.setText("");
            textField.setText("");
        });

        label.setOnMouseEntered(event -> {
            adjustEye(true);
        });
        label.setOnMouseExited(event -> {
            adjustEye(false);
        });
        label.setOnMouseClicked(event -> {
            /*密码框和文本框互换来实现密码的显示和隐藏*/
            if (passwordField.isVisible()) {
                eye.setImage(new Image(getResourceUrl("/texture/canSee.png")));
                textField.setText(passwordField.getText());
                passwordField.setVisible(false);
                textField.setVisible(true);
                textField.appendText("");
            } else {
                eye.setImage(new Image(getResourceUrl("/texture/cantSee.png")));
                passwordField.setText(textField.getText());
                passwordField.setVisible(true);
                textField.setVisible(false);
            }
        });
    }

    private void initEvent() {
        textField.textProperty().addListener((ob, ov, nv) -> {
            close.setVisible(!Objects.equals(nv, ""));
        });
        close.setOnMouseEntered(event -> adjustClose(true));
        close.setOnMouseExited(event -> adjustClose(false));
        close.setOnMouseClicked(event -> textField.setText(""));
    }

    public boolean getRight() {
        return right;
    }

    public void setRight(boolean right) {
        adjustIcon(right);
        this.right = right;
    }

    private void adjustEye(boolean b) {
        if (b) {
            eyeAdjust.setBrightness(0.4);
            eyeAdjust.setContrast(0);
            eyeAdjust.setHue(0);
            eyeAdjust.setSaturation(0);
        } else {
            eyeAdjust.setBrightness(0);
            eyeAdjust.setContrast(0);
            eyeAdjust.setHue(0);
            eyeAdjust.setSaturation(0);
        }
    }

    private void adjustClose(boolean b) {
        if (b) {
            closeAdjust.setBrightness(0.1);
            closeAdjust.setContrast(1);
            closeAdjust.setHue(0);
            closeAdjust.setSaturation(0.2);
        } else {
            closeAdjust.setBrightness(0);
            closeAdjust.setContrast(0);
            closeAdjust.setHue(0);
            closeAdjust.setSaturation(0);
        }
    }

    private void adjustIcon(boolean b) {
        if (b) {
            iconAdjust.setBrightness(0);
            iconAdjust.setContrast(0);
            iconAdjust.setHue(0);
            iconAdjust.setSaturation(0);
        } else {
            iconAdjust.setBrightness(-0.3);
            iconAdjust.setContrast(1);
            iconAdjust.setHue(7 / 9.0);
            iconAdjust.setSaturation(0);
        }
    }
}
